#!/usr/bin/env bats
#
# Copyright (c) 2019 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

load "${BATS_TEST_DIRNAME}/../../common.bash"
load "${BATS_TEST_DIRNAME}/tests_common.sh"
load "${BATS_TEST_DIRNAME}/confidential_common.sh"

setup() {
	is_confidential_runtime_class && \
		skip "See: https://github.com/kata-containers/kata-containers/issues/9663"

	pod_name="custom-dns-test"
	file_name="/etc/resolv.conf"
	get_pod_config_dir
	yaml_file="${pod_config_dir}/pod-custom-dns.yaml"

	# Add policy to the yaml file
	policy_settings_dir="$(create_tmp_policy_settings_dir "${pod_config_dir}")"

	exec_command=(cat "${file_name}")
	add_exec_to_policy_settings "${policy_settings_dir}" "${exec_command[@]}"
	add_requests_to_policy_settings "${policy_settings_dir}" "ReadStreamRequest"

	auto_generate_policy "${policy_settings_dir}" "${yaml_file}"
}

@test "Check custom dns" {
	# Create the pod
	kubectl create -f "${yaml_file}"

	# Check pod creation
	kubectl wait --for=condition=Ready --timeout=$timeout pod $pod_name

	# Check dns config at /etc/resolv.conf
	kubectl exec "$pod_name" -- "${exec_command[@]}" | grep -q "nameserver 1.2.3.4"
	kubectl exec "$pod_name" -- "${exec_command[@]}" | grep -q "search dns.test.search"
}

teardown() {
	is_confidential_runtime_class && \
		skip "See: https://github.com/kata-containers/kata-containers/issues/9663"

	# Debugging information
	kubectl describe "pod/$pod_name"

	kubectl delete pod "$pod_name"

	delete_tmp_policy_settings_dir "${policy_settings_dir}"
}
